package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.pojo.CourseUser;
import com.offcn.service.CourseUserService;
import com.offcn.util.PageUtil;
import com.offcn.util.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {
    private CourseUserService courseUserService = new CourseUserService();
    private ResultVo resultVo = null;

    public void queryAllByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        String search = request.getParameter("search");
        PageUtil<CourseUser> pageUtil = courseUserService.queryAllByPage(currentPage, pageSize, search);
        resultVo = new ResultVo(200, "查询成功", pageUtil);
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*改课功能*/
    public void updateCourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String[]> map = request.getParameterMap();
        CourseUser courseUser = new CourseUser();
        BeanUtils.populate(courseUser, map);
        int i = courseUserService.updateCourse(courseUser);
        if (i > 0) {
            resultVo = new ResultVo(200, "修改成功", null);
        } else {
            resultVo = new ResultVo(500, "修改失败", null);
        }
        response.getWriter().write(JSON.toJSONString(resultVo));
    }

    /*批量删除功能*/
    public void delAllCourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String ids = request.getParameter("ids");
        int i = courseUserService.delAllCourse(ids);
        if (i > 0) {
            resultVo = new ResultVo(200, "删除成功", null);
        } else {
            resultVo = new ResultVo(500, "删除失败", null);
        }
    }
}
